<template>
  <el-dialog
    :model-value="visible"
    title="编辑账户信息"
    width="500px"
    @close="handleClose"
  >
    <el-form
      ref="formRef"
      :model="form"
      :rules="rules"
      label-width="100px"
    >
      <el-form-item label="ID" prop="id">
        <el-input v-model="form.id" disabled />
      </el-form-item>
      
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="form.phone" disabled />
      </el-form-item>
      
      <el-form-item label="预存费用" prop="preDeposit">
        <el-input 
          v-model.number="form.preDeposit" 
          placeholder="请输入预存费用" 
          type="number"
        />
      </el-form-item>
      
      <el-form-item label="总消费" prop="totalConsumption">
        <el-input 
          v-model.number="form.totalConsumption" 
          placeholder="请输入总消费" 
          type="number"
        />
      </el-form-item>
      
      <el-form-item label="月消费" prop="monthConsumption">
        <el-input 
          v-model.number="form.monthConsumption" 
          placeholder="请输入月消费" 
          type="number"
        />
      </el-form-item>
      
      <el-form-item label="是否欠费" prop="isArrears">
        <el-select v-model="form.isArrears" placeholder="请选择是否欠费" style="width: 100%">
          <el-option label="是" :value="1" />
          <el-option label="否" :value="0" />
        </el-select>
      </el-form-item>
      
      <el-form-item label="欠费金额" prop="arrearsAmount">
        <el-input 
          v-model.number="form.arrearsAmount" 
          placeholder="请输入欠费金额" 
          type="number"
        />
      </el-form-item>
    </el-form>
    
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleSubmit" :loading="submitLoading">
          确认修改
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { updateAccountInfo } from '@/apis/account'

export default {
  name: 'EditAccount',
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  emits: ['update:visible', 'success'],
  setup(props, { emit }) {
    const formRef = ref()
    const submitLoading = ref(false)
    
    const form = reactive({
      id: '',
      phone: '',
      preDeposit: 0,
      totalConsumption: 0,
      monthConsumption: 0,
      isArrears: 0,
      arrearsAmount: 0
    })
    
    const rules = {
      id: [
        { required: true, message: 'ID不能为空', trigger: 'blur' }
      ],
      phone: [
        { required: true, message: '手机号不能为空', trigger: 'blur' }
      ],
      preDeposit: [
        { required: true, message: '请输入预存费用', trigger: 'blur' }
      ],
      totalConsumption: [
        { required: true, message: '请输入总消费', trigger: 'blur' }
      ],
      monthConsumption: [
        { required: true, message: '请输入月消费', trigger: 'blur' }
      ],
      isArrears: [
        { required: true, message: '请选择是否欠费', trigger: 'change' }
      ],
      arrearsAmount: [
        { required: true, message: '请输入欠费金额', trigger: 'blur' }
      ]
    }
    
    // 设置表单数据
    const setFormData = (data) => {
      Object.keys(form).forEach(key => {
        if (['preDeposit', 'totalConsumption', 'monthConsumption', 'arrearsAmount'].includes(key)) {
          form[key] = parseFloat(data[key]) || 0;
        } else if (['isArrears'].includes(key)) {
          form[key] = parseInt(data[key]) || 0;
        } else {
          form[key] = data[key] !== undefined ? data[key] : '';
        }
      });
    }
    
    // 关闭弹窗
    const handleClose = () => {
      emit('update:visible', false)
    }
    
    // 提交表单
    const handleSubmit = async () => {
      try {
        await formRef.value.validate()
        submitLoading.value = true
        
        // 准备请求数据
        const requestData = {
          id: form.id,
          phone: form.phone,
          preDeposit: parseFloat(form.preDeposit) || 0,
          totalConsumption: parseFloat(form.totalConsumption) || 0,
          monthConsumption: parseFloat(form.monthConsumption) || 0,
          isArrears: parseInt(form.isArrears),
          arrearsAmount: parseFloat(form.arrearsAmount) || 0
        }
        
        const res = await updateAccountInfo(requestData)
        if (res && res.code === 0) {
          ElMessage.success('账户信息修改成功')
          emit('success')
          handleClose()
        } else {
          ElMessage.error(res?.msg || '修改失败')
        }
      } catch (error) {
        console.error('修改账户信息失败:', error)
        ElMessage.error('修改账户信息失败')
      } finally {
        submitLoading.value = false
      }
    }
    
    // 暴露方法给父组件
    return {
      formRef,
      form,
      rules,
      submitLoading,
      setFormData,
      handleClose,
      handleSubmit
    }
  }
}
</script>

<style scoped>
.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
</style>